package com.das.mechanic_base.adapter.visit;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：zhanglei on 2019/10/11 11:45
 * <p>
 * 邮箱：916256628@qq.com
 */
public class X3VisitStoreAdapter extends RecyclerView.Adapter<X3VisitStoreAdapter.TagViewHolder> {

    private Context mContext;

    private ItemClickListener itemClickListener;

    private List<String> mList;

    private List<Boolean> sList;

    public X3VisitStoreAdapter(Context context) {
        this.mContext = context;
        mList = new ArrayList<>();
        this.sList = new ArrayList<>();
    }

    public void changeTagData(List<String> mList) {
        this.mList = mList;
        this.sList.clear();
        for (int i = 0; i < mList.size(); i++) {
            this.sList.add(false);
        }
        notifyDataSetChanged();
    }

    public void changeSelectStatus(String select) {
        int selection = -1;
        for (int i = 0; i < mList.size(); i++) {
            if (select.equals(mList.get(i))) {
                selection = i;
            }
        }

        if (selection != -1) {
            for (int i = 0; i < sList.size(); i++) {
                sList.remove(i);
                sList.add(i,i == selection);
            }
        }
        notifyDataSetChanged();
    }

    @Override
    public TagViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new TagViewHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_gv_idea_item_adapter, parent, false));
    }

    @Override
    public void onBindViewHolder(TagViewHolder holder, int position) {
        holder.tv_title.setText(mList.get(position));

        if (sList.get(position)) {
            holder.tv_title.setBackgroundResource(R.drawable.x3_shape_idea_select_item);
            holder.tv_title.setTextColor(Color.parseColor("#0077FF"));
        } else {
            holder.tv_title.setBackgroundResource(R.drawable.x3_shape_idea_unselect_item);
            holder.tv_title.setTextColor(Color.parseColor("#333333"));
        }

        holder.itemView.setOnClickListener(v -> {
            for (int i = 0; i < sList.size(); i++) {
                sList.remove(i);
                sList.add(i,i == position);
            }

            if (itemClickListener != null) {
                itemClickListener.itemClick(position);
            }
            notifyDataSetChanged();
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class TagViewHolder extends RecyclerView.ViewHolder {
        TextView tv_title;
        public TagViewHolder(View itemView) {
            super(itemView);
            tv_title = itemView.findViewById(R.id.tv_title);
        }
    }

    public interface ItemClickListener {
        void itemClick(int position);
    }

    public ItemClickListener getItemClickListener() {
        return itemClickListener;
    }

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }
}
